package com.ruoyi.project.order.deposit.service.impl;


import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.config.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.order.deposit.mapper.DepositMapper;
import com.ruoyi.project.order.deposit.domain.Deposit;
import com.ruoyi.project.order.deposit.service.IDepositService;
import com.ruoyi.common.utils.text.Convert;
import com.ruoyi.project.system.info.mapper.RoomInfoMapper;
import com.ruoyi.project.system.info.domain.RoomInfo;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 押金记录Service业务层处理
 * 
 * @author yyj
 * @date 2021-05-13
 */
@Service
public class DepositServiceImpl implements IDepositService 
{
    @Autowired
    private DepositMapper depositMapper;
    @Autowired
    private RoomInfoMapper roomInfoMapper;
    @Autowired
    private RedisUtil redisUtil;

    private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

    private String getDate(int n){
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date());
        if(n==0) return sdf.format(cal.getTime());
        cal.set(Calendar.DATE, cal.get(Calendar.DATE)-n);
        return sdf.format(cal.getTime());
    }

    public List<Object[]> getMonthReport(String type){
        List<Object[]> list=new LinkedList<>();
        String[] dates=new String[31];
        Integer[] datas=new Integer[31];
        list.add(dates);list.add(datas);
        Integer num=null;
        Integer total=0;
        for(int i=0;i<30;i++){
            String date=getDate(i);
            if(!type.equals("")) date+=type;
            dates[29-i]=getDate(i);
            if((num = (Integer)redisUtil.get(date))==null) datas[29-i]=0;
            else {
                datas[29-i]=num;
                total+=num;
            }
        }
        dates[30]="total";
        datas[30]=total;
        return list;
    }

    /**
     * 查询押金记录
     * 
     * @param depositId 押金记录ID
     * @return 押金记录
     */
    @Override
    public Deposit selectDepositById(Long depositId)
    {
        return depositMapper.selectDepositById(depositId);
    }

    /**
     * 查询押金记录列表
     * 
     * @param deposit 押金记录
     * @return 押金记录
     */
    @Override
    public List<Deposit> selectDepositList(Deposit deposit)
    {
        return depositMapper.selectDepositList(deposit);
    }

    /**
     * 新增押金记录
     * 
     * @param deposit 押金记录
     * @return 结果
     */
    @Override
    public int insertDeposit(Deposit deposit)
    {
        deposit.setCreateTime(DateUtils.getNowDate());
        RoomInfo roomInfo=new RoomInfo();
        roomInfo.setRoomNumber(deposit.getRoomNumber());
        List<RoomInfo> list=roomInfoMapper.selectRoomInfoList(roomInfo);
        List<String> ids=new LinkedList<>();
        ids.add(Long.toString(list.get(0).getRoomId()));
        roomInfoMapper.updateRoomStatusByIds(ids,(long)2,list.get(0).getRemark());
        String date=getDate(0);
        Integer num=null;
        if((num = (Integer)redisUtil.get(date))==null)
            redisUtil.set(date,1);
        else redisUtil.set(date,num+1);
        date+=list.get(0).getRoomTypeId();
        if((num = (Integer)redisUtil.get(date))==null)
            redisUtil.set(date,1);
        else redisUtil.set(date,num+1);
        return depositMapper.insertDeposit(deposit);
    }

    /**
     * 修改押金记录
     * 
     * @param deposit 押金记录
     * @return 结果
     */
    @Override
    public int updateDeposit(Deposit deposit)
    {
        deposit.setUpdateTime(DateUtils.getNowDate());
        if(deposit.getDepositStatus()==2){
            //入住切换成预约
            RoomInfo roomInfo=new RoomInfo();
            roomInfo.setRoomNumber(deposit.getRoomNumber());
            List<RoomInfo> list=roomInfoMapper.selectRoomInfoList(roomInfo);
            List<String> ids=new LinkedList<>();
            ids.add(Long.toString(list.get(0).getRoomId()));
            roomInfoMapper.updateRoomStatusByIds(ids,(long)0,"");
        }
        return depositMapper.updateDeposit(deposit);
    }

    /**
     * 删除押金记录对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteDepositByIds(String ids)
    {
        return depositMapper.deleteDepositByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除押金记录信息
     * 
     * @param depositId 押金记录ID
     * @return 结果
     */
    @Override
    public int deleteDepositById(Long depositId)
    {
        return depositMapper.deleteDepositById(depositId);
    }
}
